﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBMode
{
    public class RecordMethod:IMethods<Record>
    {
        public int Add(Record t)
        {
            using (CMSDBEntities cMSDB = new CMSDBEntities())
            {
                if (t == null) return 0;
                t.CreateTime = DateTime.Now;
                cMSDB.Record.Add(t);
                int a = cMSDB.SaveChanges();
                return a;
            }
        }

        public int Delete(Record t)
        {
            using (CMSDBEntities cMSDB = new CMSDBEntities())
            {
                int a = 0;
                var model = cMSDB.Record.ToList().FirstOrDefault(i => i.ComName == t.ComName);
                if (model != null)
                {
                    cMSDB.Record.Remove(model);
                    a = cMSDB.SaveChanges();
                }
                return a;
            }
        }

        public List<Record> Query()
        {
            using (CMSDBEntities cMSDB = new CMSDBEntities())
            {
                return cMSDB.Record.ToList();
            }
        }

        public int Update(Record t)
        {
            using (CMSDBEntities cMSDB = new CMSDBEntities())
            {
                int a = 0;
                if (t == null) return 0;
                var model = cMSDB.Record.ToList().FirstOrDefault(i => i.Id == t.Id);
                if (model != null)
                {                   
                    model.Type = t.Type;
                    model.Number = t.Number;
                    model.Tag = t.Tag;
                    a = cMSDB.SaveChanges();
                    return a;
                }
                return a;
            }
        }
    }
}
